<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="边坡编号" prop="slopeId">
        <el-select v-model="queryParams.slopeId" placeholder="请选择边坡编号" clearable style="width: 200px">
          <el-option
              v-for="dict in slopeCodeList"
              :key="dict.value"
              :label="dict.value"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="监测点ID" prop="monitorId">
        <el-input
            v-model="queryParams.monitorId"
            placeholder="请输入监测点ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="queryParams.deviceType" placeholder="请选择设备类型" clearable style="width: 180px">
          <el-option
            v-for="dict in device_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备SN" prop="deviceSn">
        <el-input
          v-model="queryParams.deviceSn"
          placeholder="请输入设备SN"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备id" prop="deviceId">
        <el-input
          v-model="queryParams.deviceId"
          placeholder="请输入设备id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['video.device:device:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['video.device:device:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['video.device:device:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['video.device:device:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="边坡编号" fixed="left" align="center" prop="slopeId" width="280px">
        <template #default="scope">
          <dict-tag :options="slopeCodeList" :value="scope.row.slopeId"/>
        </template>
      </el-table-column>
      <el-table-column label="监测点ID" align="center" prop="monitorId" />
      <el-table-column label="设备类型" align="center" prop="deviceType">
        <template #default="scope">
          <dict-tag :options="device_type" :value="scope.row.deviceType"/>
        </template>
      </el-table-column>
      <el-table-column label="监测类型" align="center" prop="monitorType">
        <template #default="scope">
          <dict-tag :options="monitor_type" :value="scope.row.monitorType"/>
        </template>
      </el-table-column>
      <el-table-column label="安装日期" align="center" prop="installationDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.installationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="安装位置" align="center" prop="installationLocation">
        <template #default="scope">
          <dict-tag :options="monitor_area" :value="scope.row.installationLocation"/>
        </template>
      </el-table-column>
      <el-table-column label="设备SN" align="center" prop="deviceSn" />
      <el-table-column label="设备id" align="center" prop="deviceId" />
      <el-table-column label="设备密钥" align="center" prop="deviceKey">
        <template #default="{ row }">
    <span>
      {{ row._showKey ? row.deviceKey : '****' }}
    </span>
          <el-icon
              @click="row._showKey = !row._showKey"
              class="cursor-pointer"
              style="margin-left: 8px;"
          >
            <View v-if="row._showKey" />
            <Hide v-else />
          </el-icon>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['video.device:device:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['video.device:device:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改视频设备列表对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="deviceRef" :model="form" :rules="rules" label-width="100px">
        <el-col :span="12">
          <el-form-item label="边坡编号" prop="slopeId" style="width: 380px;">
            <el-select v-model="form.slopeId" placeholder="请选择边坡编号">
              <el-option
                  v-for="dict in slopeCodeList"
                  :key="dict.value"
                  :label="dict.value"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-form-item label="监测点ID" prop="monitorId">
          <el-input v-model="form.monitorId" placeholder="请输入监测点ID" />
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          <el-select v-model="form.deviceType" placeholder="请选择设备类型">
            <el-option
              v-for="dict in device_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="监测类型" prop="monitorType">
          <el-select v-model="form.monitorType" placeholder="请选择监测类型">
            <el-option
              v-for="dict in monitor_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="安装日期" prop="installationDate">
          <el-date-picker clearable
            v-model="form.installationDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择安装日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="安装位置" prop="installationLocation">
          <el-select v-model="form.installationLocation" placeholder="请选择安装位置">
            <el-option
              v-for="dict in monitor_area"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备SN" prop="deviceSn">
          <el-input v-model="form.deviceSn" placeholder="请输入设备SN" />
        </el-form-item>
        <el-form-item label="设备id" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备id" />
        </el-form-item>
        <el-form-item label="设备密钥" prop="deviceKey">
          <el-input
              v-model="form.deviceKey"
              type="password"
              placeholder="请输入设备密钥"
              show-password
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Device">
import { listDevice, getDevice, delDevice, addDevice, updateDevice } from "@/api/slope/video/device/device";
import {listSlopeCode} from "@/api/slope/info/info.js";

const { proxy } = getCurrentInstance();
const { monitor_type, device_type, monitor_area } = proxy.useDict('monitor_type', 'device_type', 'monitor_area');

const slopeCodeList = ref([])
const deviceList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    slopeId: null,
    monitorId: null,
    deviceType: null,
    deviceSn: null,
    deviceId: null,
  },
  rules: {
    slopeId: [
      { required: true, message: "边坡ID不能为空", trigger: "change" }
    ],
    monitorId: [
      { required: true, message: "监测点ID不能为空", trigger: "change" }
    ],
    deviceSn: [
      { required: true, message: "设备SN不能为空", trigger: "blur" }
    ],
    deviceId: [
      { required: true, message: "设备id不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询视频设备列表列表 */
function getList() {
  loading.value = true;
  listSlopeCode().then(response => {
    // 假设 response 是一个普通的对象
    slopeCodeList.value = Object.entries(response).map(([_, value]) => ({
      label: value,
      value: value
    }))
  })
  listDevice(queryParams.value).then(response => {
    deviceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    monitorId: null,
    deviceType: null,
    monitorType: null,
    installationDate: null,
    installationLocation: null,
    deviceSn: null,
    deviceId: null,
    updateTime: null,
    deptId: null
  };
  proxy.resetForm("deviceRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加视频设备列表";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getDevice(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改视频设备列表";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["deviceRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateDevice(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDevice(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除视频设备列表编号为"' + _ids + '"的数据项？').then(function() {
    return delDevice(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('video.device/device/export', {
    ...queryParams.value
  }, `device_${new Date().getTime()}.xlsx`)
}

getList();
</script>
